define(function (require,exports,modules) {
    require("cookie");
    function login() {
        let data = {};
        let reg =/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/ ;

        $("form").submit(function () {
            if(reg.test($("#uname").val()))
            {
                data.uemail=$("#uname").val();
            }
            else{
                data.uname=$("#uname").val();
            }
            data.upwd = $("#upwd").val();
            $.ajax({
                    url:"../../server/login.php",
                    data:data,
                    type:"post",
                    dataType:"json"
                }).then(function (res) {
                        if(res.status==201){
                            sessionStorage.setItem("userInfo",JSON.stringify(res.data));
                            alert(res.msg);
                            let cookieList=JSON.parse($.cookie("carts")||"[]");
                            var arr=[];
                            console.log(cookieList)
                            cookieList.forEach((ele,index)=>{
                               ele.u_id=res.data.u_id;
                               ele.img=ele.img[0];
                                console.log(ele);
                                let p=$.ajax(
                                    {
                                        url:"../../server/addCart.php",
                                        type:"post",
                                        data:ele
                                    }
                                );
                                arr.push(p.promise());
                            })
                            $.cookie("carts","",{expires:-1});
                            // Promise.all(arr).then(function (res) {
                            //
                            // })
                            location="index1.html";
                        }
                        else {
                            alert(res.msg)
                        }
                })


            return false;
        })

    }
    exports.login=login;

})